package com.hzqy.service.hotel;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.utils.HttpUtil;
import com.hzqy.service.ift.XpYunServiceBean;
import com.hzqy.service.pms.PmsServiceBean;
import com.hzqy.web.vo.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hzqy.commons.utils.ConstantUtils;
import com.hzqy.commons.utils.JsonUtils;
import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.commons.utils.sensitive.SensitiveMain;
import com.hzqy.persistence.dao.content.ContentDao;
import com.hzqy.persistence.dao.hotel.WchatHotelDao;
import com.hzqy.persistence.dao.picture.PictureDao;
import com.hzqy.persistence.dao.pms.PmsDao;

@Service
public class WechatHotelService {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	final private int CATALOG_KFFW = 855;//客房服务
	final private int CATALOG_DCFW = 857;//点餐服务
	final private int CATALOG_GWFW = 859;//购物服务
	final private int CATALOG_TSJY = 861;//投诉建议
	
	@Autowired
	private WchatHotelDao wchatHotelDao;
	
	@Autowired
	private ContentDao contentDao;
	
	@Autowired
	private PmsDao pmsDao;
	
	@Resource
	private PictureDao pictureDao;

	@Autowired
	private PmsServiceBean pmsServiceBean;

	public List<CmContentVo> selectContentListByCatalogId(int f_catalog_id,int f_po_id,int pageNo,int pageSize,String f_cp_cprid,CmContentVo contentVo) {
		List<CmContentVo> results = wchatHotelDao.selectContentListByCatalogId(f_catalog_id,f_po_id,pageNo,pageSize,f_cp_cprid,contentVo);
		return results;
	}
	
	public List<CmContentVo> selectContentListByCatalogIdThree(int f_catalog_id,int pageNo,int pageSize,String f_cp_cprid,CmContentVo contentVo) {
		List<CmContentVo> results = wchatHotelDao.selectContentListByCatalogIdThree(f_catalog_id,pageNo,pageSize,f_cp_cprid,contentVo);
		return results;
	}
	
	public List<CmContentVo> selectContentListByCatalogIdNoTime(int f_catalog_id,int f_po_id,int pageNo,int pageSize,String f_cp_cprid,CmContentVo contentVo) {
		List<CmContentVo> results = wchatHotelDao.selectContentListByCatalogIdNoTime(f_catalog_id,f_po_id,pageNo,pageSize,f_cp_cprid,contentVo);
		return results;
	}
	
	public int selectUserCountByName(String f_pu_name ,String f_pu_phone,String f_pu_pwd) {
		return wchatHotelDao.selectUserCountByName(f_pu_name,f_pu_phone,f_pu_pwd);
	}
	
	public int setOpenid(String f_pu_email,String f_up_name) {
		return wchatHotelDao.setOpenid(f_pu_email,f_up_name);
	}
	
	public int updateOrgMmodel(int f_po_id,int f_po_model,String f_po_model_name) {
		return wchatHotelDao.updateOrgMmodel(f_po_id,f_po_model,f_po_model_name);
	}
	
	public List<PmsOrgVo> selectPms_org(PmsOrgVo pms_org) {
		return wchatHotelDao.selectPms_org(pms_org);
	}
	
	public int select_count_hotel() {
		return wchatHotelDao.select_count_hotel();
	}
	
	public int selectOrg_Content(String f_po_name,String f_cc_name) {
		return wchatHotelDao.selectOrg_Content(f_po_name,f_cc_name);
	}
	
	public Map<String,Object> insertContentAndOrg(HttpSession session,PmsOrgVo pov,CmContentVo cmContentVo,CmCatalogContentVo cmCatalogContentVo,int f_pou_userId) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String, Object> name = SensitiveMain.sensitiveWordCheck(cmContentVo.getF_cc_name());
		Map<String, Object> desc = SensitiveMain.sensitiveWordCheck(cmContentVo.getF_cc_desc());
		Map<String, Object> orgName = SensitiveMain.sensitiveWordCheck(pov.getF_po_name());
		if(!(Boolean)name.get("hasSensitiveWord")&&!(Boolean)desc.get("hasSensitiveWord")&&!(Boolean)orgName.get("hasSensitiveWord")){
			int a =  pmsDao.insertOrg(pov);
			if(a==1){
				LOG.debug("wx-新用户开户:酒店-"+pov.getF_po_name()+"新增成功");
			}else{
				LOG.debug("wx-新用户开户:酒店-"+pov.getF_po_name()+"新增失败");
			}
			int f_po_id = pov.getF_po_id();
			cmContentVo.setF_cc_companyid(f_po_id+"");
			CmContentVo newCmContentVo = new CmContentVo();
			newCmContentVo.setF_cc_name(cmContentVo.getF_cc_code());
			insertUser(newCmContentVo,f_po_id,Integer.parseInt(cmContentVo.getF_cc_tags()));//新增itv账号
			cmContentVo.setF_cc_tags("");
			cmContentVo.setF_cc_code("");
			int b = wchatHotelDao.insertContent(cmContentVo);//微信业主信息
			int f_cc_id = cmContentVo.getF_cc_id();
			wchatHotelDao.insertContentOrg(f_cc_id, cmContentVo.getF_cc_companyid());
			if(b==1){
				LOG.debug("wx-新用户开户:内容-"+cmContentVo.getF_cc_name()+"新增成功");
			}else{
				LOG.debug("wx-新用户开户:内容-"+cmContentVo.getF_cc_name()+"新增失败");
			}
			insertPmsOrgUser(f_pou_userId,f_po_id);
			cmCatalogContentVo.setF_ccc_contentid(f_cc_id);
			contentDao.insertBind(cmCatalogContentVo);
			
			resultMap.put("success", true);
			resultMap.put("result","新增成功!");
		}else if((Boolean)name.get("hasSensitiveWord")){
			resultMap.put("success", false);
			resultMap.put("result","新增失败!含有敏感词："+name.get("sensitiveWordSet").toString());
		}else if((Boolean)desc.get("hasSensitiveWord")){
			resultMap.put("success", false);
			resultMap.put("result","新增失败!含有敏感词："+desc.get("sensitiveWordSet").toString());
		}else if((Boolean)orgName.get("hasSensitiveWord")){
			resultMap.put("success", false);
			resultMap.put("result","新增失败!含有敏感词："+orgName.get("sensitiveWordSet").toString());
		}
		
		return resultMap;
	}
	
	public void insertUser(CmContentVo cmContentVo, int f_po_id,int f_catalog_id) {
		cmContentVo.setF_cc_type(100);
		int count = contentDao.findContentCount(cmContentVo);
		if(count==0){
			CmCatalogContentVo cmCatalogContentVo = new CmCatalogContentVo();
			cmCatalogContentVo.setF_ccc_catalogid(f_catalog_id);
			cmContentVo.setF_cc_status(1);
			cmContentVo.setF_cc_interfaceType(1);
			cmContentVo.setF_cc_onlinedate("2009-09-19 14:28:11");
			cmContentVo.setF_cc_offlinedate("2099-09-19 14:28:11");
			wchatHotelDao.insertContent(cmContentVo);
			int f_cc_id = cmContentVo.getF_cc_id();
			wchatHotelDao.insertContentOrg(f_cc_id, f_po_id+"");
			cmCatalogContentVo.setF_ccc_contentid(f_cc_id);
			contentDao.insertBind(cmCatalogContentVo);
		}
	}
	
	public int insertPmsOrgUser(int f_pou_userId, int f_pou_orgId) {
		return wchatHotelDao.insertPmsOrgUser(f_pou_userId,f_pou_orgId);
	}
	
	public List<CmContentVo> selectContentByCompanyid(String f_catalog_id,String f_cco_orgId) {
		return wchatHotelDao.selectContentByCompanyid(f_catalog_id,f_cco_orgId);
	}
	
	public Map<String,Object> insertContent(HttpSession session, CmContentVo cmContentVo, int catalogid,String picJson) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = 0;
		if(cmContentVo==null || cmContentVo.getF_cc_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			Map<String, Object> name = SensitiveMain.sensitiveWordCheck(cmContentVo.getF_cc_name());
			Map<String, Object> desc = SensitiveMain.sensitiveWordCheck(cmContentVo.getF_cc_desc());
			if(!(Boolean)name.get("hasSensitiveWord")&&!(Boolean)desc.get("hasSensitiveWord")){
				int b = wchatHotelDao.insertContent(cmContentVo);
				int f_cc_id = cmContentVo.getF_cc_id();
				String arr[] = cmContentVo.getF_cc_companyid().split(",");
				for(int i=0;i<arr.length;i++){
					wchatHotelDao.insertContentOrg(f_cc_id, arr[i]);
				}
				if(b==1){
					LOG.debug("wx:内容-"+cmContentVo.getF_cc_name()+"新增成功");
				}else{
					LOG.debug("wx:内容-"+cmContentVo.getF_cc_name()+"新增失败");
				}
				if(picJson==null||picJson.equals("")){
					
				}else{
					List<CmPicturesVo> picList = JsonUtils.jsonToPicVo(picJson);
					LOG.debug("id  "+f_cc_id);
					for(int i=0;i<picList.size();i++) {
						CmPicturesVo cmPicturesVo = new CmPicturesVo();
						cmPicturesVo = picList.get(i);
						cmPicturesVo.setF_cp_relationid(f_cc_id);
						pictureDao.insertPic(cmPicturesVo);
					}
				}
				
				CmCatalogContentVo cmCatalogContentVo = new CmCatalogContentVo();
				cmCatalogContentVo.setF_ccc_contentid(cmContentVo.getF_cc_id());
				cmCatalogContentVo.setF_ccc_catalogid(catalogid);
				wchatHotelDao.insertBind(cmCatalogContentVo);

				resultMap.put("success", true);
				resultMap.put("result","新增内容成功!");
				resultMap.put("f_cc_id",f_cc_id);
			}else if((Boolean)name.get("hasSensitiveWord")){
				resultMap.put("success", false);
				resultMap.put("result","新增内容失败!含有敏感词："+name.get("sensitiveWordSet").toString());
			}else if((Boolean)desc.get("hasSensitiveWord")){
				resultMap.put("success", false);
				resultMap.put("result","新增内容失败!含有敏感词："+desc.get("sensitiveWordSet").toString());
			}
		}else{
			resultMap.put("success", false);
			resultMap.put("result","新增内容失败!");
		}
		return resultMap;
	}
	
	public List<PmsUserVo> selectOpenid(HttpSession session,String f_pu_email){
		List<PmsUserVo> pmsUser = wchatHotelDao.selectOpenid(f_pu_email);
		if(pmsUser.isEmpty()){
			LOG.debug("wx:不存在此openid，openid为"+f_pu_email);
		}else{
			session.setAttribute("pmsUserVo", pmsUser.get(0));
			LOG.debug("wx:用户信息已存入session，号码为："+pmsUser.get(0).getF_pu_name());
			List<PmsOrgVo> pmsOrgVoList = wchatHotelDao.selectHotelListByManager(pmsUser.get(0).getF_pu_id(), 0);
			if(!pmsOrgVoList.isEmpty()){
				session.setAttribute("pmsOrgVo",pmsOrgVoList.get(0));
				LOG.debug("wx:获取组织信息成功，用户名："+pmsUser.get(0).getF_pu_name()+",组织id为"+pmsOrgVoList.get(0).getF_po_id());
			}else{
				LOG.debug("wx:获取组织信息失败，用户名："+pmsUser.get(0).getF_pu_name());
			}
		}
		return pmsUser;
	}
	
	public List<PmsOrgVo> selectHotelListByManager(int f_pu_id, int f_po_id) {
		List<PmsOrgVo> pmsOrgVoList = wchatHotelDao.selectHotelListByManager(f_pu_id, f_po_id);
		if(!pmsOrgVoList.isEmpty()){
			for(int i=0;i<pmsOrgVoList.size();i++){
				List<PmsUserVo> pmsUserVoList = selectHotelUserList(pmsOrgVoList.get(i).getF_po_id());
				pmsOrgVoList.get(i).setPmsUserVoList(pmsUserVoList);
			}
		}
		return pmsOrgVoList;
	}

	public List<PmsUserVo> selectHotelUserList(int f_po_id) {
		return wchatHotelDao.selectHotelUserList(f_po_id);
	}
	
	public int selectContentCount(int f_cc_type) {
		return wchatHotelDao.selectContentCount(f_cc_type);
	}
	
	public int deleteHotelUser(int f_pu_id) {
		return wchatHotelDao.deleteHotelUser(f_pu_id);
	}
	
	public List<PmsOrgVo> selectOrgNameById(String f_pu_id) {
		return wchatHotelDao.selectOrgNameById(f_pu_id);
	}

	public int insertUser(HttpSession session,PmsUserVo pmsUserVo, int f_po_id,int user_count) {
		int flag = wchatHotelDao.insertUser(pmsUserVo);
		PmsOrgUserVo pmsOrgUserVo = new PmsOrgUserVo();
		pmsOrgUserVo.setF_pou_orgId(f_po_id);
		pmsOrgUserVo.setF_pou_userId(pmsUserVo.getF_pu_id());
		pmsDao.insertOrgUser(pmsOrgUserVo);
		flag = updateOrgUserCount(session,f_po_id,user_count);
		return flag;
	}
	
	public int updateUser(HttpSession session,PmsUserVo pmsUserVo, int f_po_id,int user_count) {
		int flag = wchatHotelDao.updateUser(pmsUserVo);
		flag = updateOrgUserCount(session,f_po_id,user_count);
		return flag;
	}
	
	public int updateOrgUserCount(HttpSession session,int f_po_id,int user_count){
		int flag = 1;
		if(user_count>0){
			flag = wchatHotelDao.updateOrgUserCount(f_po_id,user_count);
			PmsOrgVo pmsOrgVo = (PmsOrgVo) session.getAttribute("pmsOrgVo");
			pmsOrgVo.setUser_count(user_count);
			session.setAttribute("pmsOrgVo",pmsOrgVo);
		}
		return flag;
	}
	
	public int insertManager(HttpSession session,PmsUserVo pmsUserVo) {
		int flag = wchatHotelDao.insertUser(pmsUserVo);
		int f_pu_id = pmsUserVo.getF_pu_id();
		LOG.debug("经理f_pu_id:"+f_pu_id);
		session.setAttribute("pmsUserVo",pmsUserVo);
		return flag;
	}

	public Map<String,Object> updateContent(CmContentVo cmContentVo ) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String, Object> desc = SensitiveMain.sensitiveWordCheck(cmContentVo.getF_cc_desc());
		if(!(Boolean)desc.get("hasSensitiveWord")){
			wchatHotelDao.updateContent(cmContentVo);
			resultMap.put("success", true);
			resultMap.put("result","修改内容成功!");
		}else{
			resultMap.put("success", false);
			resultMap.put("result","修改内容失败!含有敏感词："+desc.get("sensitiveWordSet").toString());
		}
		
		return resultMap;
	}

	public Map<String,Object> updateContentDescPic(CmContentVo cmContentVo ) {
		if(cmContentVo.getF_cc_desc().indexOf("_ueditor_page_break_tag_")>-1){
			String[] picurlArr = cmContentVo.getF_cc_desc().split("_ueditor_page_break_tag_");
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i<picurlArr.length;i++){
				sb.append("<P><img src=\"").append(picurlArr[i]).append("\" /></p>");
				if(i!=picurlArr.length-1){
					sb.append("'_ueditor_page_break_tag_'");
				}
			}
			cmContentVo.setF_cc_desc(sb.toString());
		}

		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String, Object> desc = SensitiveMain.sensitiveWordCheck(cmContentVo.getF_cc_desc());
		if(!(Boolean)desc.get("hasSensitiveWord")){
			wchatHotelDao.updateContent(cmContentVo);
			resultMap.put("success", true);
			resultMap.put("result","修改内容成功!");
		}else{
			resultMap.put("success", false);
			resultMap.put("result","修改内容失败!含有敏感词："+desc.get("sensitiveWordSet").toString());
		}

		return resultMap;
	}

	public CmContentVo selectPic(int f_cc_id) {
		return wchatHotelDao.selectPic(f_cc_id);
	}

	/**
	 * 修改内容 pic
	 * @param contentVo
	 * @return
	 */
	public int updateContentPic(CmContentVo contentVo,String picJson,String picType) {
		int flag = 0;
		if(contentVo==null) {
			flag = -1;
		}
		if(flag ==0) {
			List<CmPicturesVo> picList = JsonUtils.jsonToPicVo(picJson);
			String listPicId = JsonUtils.jsonToPicIdList(picJson);
			int f_cv_relationid = contentVo.getF_cc_id();
			contentDao.updateContentStatus(f_cv_relationid, 2);
			Map<String,Object> picMap = new HashMap<String, Object>();
			picMap.put("listPicId", listPicId);
			picMap.put("f_cp_relationid", f_cv_relationid);
			picMap.put("picType", picType);
			
			pictureDao.deletePic(picMap);
			for(int i=0;i<picList.size();i++) {
				CmPicturesVo cmPicturesVo = new CmPicturesVo();
				cmPicturesVo = picList.get(i);
				cmPicturesVo.setF_cp_relationid(f_cv_relationid);
				pictureDao.insertPic(cmPicturesVo);
			}
			return 1;
		}
		return flag;
	}

	public int user_handover(int lostuserid, int getuserid) {
		return wchatHotelDao.user_handover(lostuserid, getuserid);
	}
	
	public int deleteContent(int f_cc_id) {
		wchatHotelDao.deleteContent(f_cc_id);
		return 1;
	}
	
	public List<CmContentVo> selectContent(CmContentVo cmContentVo) {
		return wchatHotelDao.selectContent(cmContentVo);
	}

	public List<StatDataTotalTopRankingVo> selectTopRanking(StatDataTotalTopRankingVo statDataTotalTopRankingVo) {
		return wchatHotelDao.selectTopRanking(statDataTotalTopRankingVo);
	}

	public List<StatDataTotalTopRankingVo> selectTopRankingOrgRoomData(StatDataTotalTopRankingVo statDataTotalTopRankingVo) {
		return wchatHotelDao.selectTopRankingOrgRoomData(statDataTotalTopRankingVo);
	}

	public List<StatDataTotalTopRankingVo> selectTopRankingOrgRoomDataMonth(StatDataTotalTopRankingVo statDataTotalTopRankingVo) {
		return wchatHotelDao.selectTopRankingOrgRoomDataMonth(statDataTotalTopRankingVo);
	}
	public PmsUserVo selectUser(int f_pu_id) {
		return wchatHotelDao.selectUser(f_pu_id);
	}
	
	public String selectBICPOrgByServerid(String serverid) {
		return wchatHotelDao.selectBICPOrgByServerid(serverid);
	}
	
	public PmsOrgVo selectOrgById(int f_po_id) {
		return wchatHotelDao.selectOrgById(f_po_id);
	}
	
	public int insertHotelUserNeedsVo(PmsOrgVo poVo,HotelUserNeedsVo vo) {
		if(poVo!=null){
			vo.setF_hsn_orgId(poVo.getF_po_id());
		}
		vo.setF_hsn_sn(getPrintSN(vo));
		if(vo.getF_hsn_printStatus()==0){
			vo.setF_hsn_printStatus(1);
		}
//		JSONObject jo= (JSONObject) JSONObject.toJSON(vo);
//		HttpUtil.postJson(SystemConfigUtils.getSysConfig("print_admin_url"),jo.toString());
		return wchatHotelDao.insertHotelUserNeedsVo(vo);
	}

	public Map<String,Object> selectHotelUserNeedsList(HotelUserNeedsVo hotelUserNeedsVo, int curPage,
													   int pageSize,String statuss,String types,String orderBy_column) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("firstResult", (curPage-1)*pageSize);
		map.put("maxResults", pageSize);
		map.put("startTime", hotelUserNeedsVo.getStartTime());
		map.put("endTime", hotelUserNeedsVo.getEndTime());
		map.put("serverid", hotelUserNeedsVo.getServerid());
		map.put("roonNum", hotelUserNeedsVo.getRoonNum());
		map.put("f_hsn_orgId", hotelUserNeedsVo.getF_hsn_orgId());
		map.put("type", hotelUserNeedsVo.getType());
		map.put("status", hotelUserNeedsVo.getStatus());
		map.put("star", hotelUserNeedsVo.getStar());
		map.put("f_hsn_printStatus", hotelUserNeedsVo.getF_hsn_printStatus());
		map.put("statuss", statuss);//多个status 用in查
		map.put("types", types);//types 用in查
		if(orderBy_column!=null&&!"".equals(orderBy_column.trim())){
			map.put("orderBy_column", orderBy_column);
		}else{
			map.put("orderBy_column", "f_hsn_cdate asc");
		}
		List<HotelUserNeedsVo> results = wchatHotelDao.selectHotelUserNeedsList(map);
		if(!results.isEmpty()){
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<HotelUserNeedsVo>());
		}
		return map;
	}
	
	public List<PmsOrgVo> selectOrgByContentId(int f_cc_id) {
		return wchatHotelDao.selectOrgByContentId(f_cc_id);
	}

	public Map<String,Object> selectDivideUserBillSumOrg(Map<String,Object> map) {
	    return wchatHotelDao.selectDivideUserBillSumOrg(map);
	}
	public List<StatDataOndemandVo> selectStatDataOndemandVo(Map<String,Object> map) {
	    return wchatHotelDao.selectStatDataOndemandVo(map);
	}

	public Map<String,Object> hotelUserLogin(HttpSession session,String serverid){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		if(!"".equals(serverid)&&serverid!=null){
			CmContentVo cmContentVo = new CmContentVo();
			cmContentVo.setF_cc_name(serverid);
			cmContentVo.setF_cc_type(100);
			cmContentVo.setF_cc_status(1);
			List<CmContentVo> cmContentVoList = selectContent(cmContentVo);
			if(cmContentVoList.isEmpty()){
				LOG.debug("解析失败，未查到此业务账号："+serverid);
			}else{
				CmContentVo result = cmContentVoList.get(0);
				session.setAttribute("f_hsn_serverid", serverid);
				session.setAttribute("f_hsn_roonNum", result.getF_cc_obligate1());
				resultMap.put("f_hsn_serverid", serverid);
				resultMap.put("f_hsn_roonNum", result.getF_cc_obligate1());
				List<PmsOrgVo> pmsOrgVoList = selectOrgByContentId(result.getF_cc_id());
				if(pmsOrgVoList.isEmpty()){
					LOG.debug("未查到对应组织信息，业务账号为："+serverid);
				}

				session.setAttribute("pmsOrgVo",pmsOrgVoList.get(0));
				resultMap.put("f_po_id", pmsOrgVoList.get(0).getF_po_id());
				resultMap.put("f_po_name", pmsOrgVoList.get(0).getF_po_name());
				resultMap.put("img_url", SystemConfigUtils.getSysConfig("wx_img_url"));
				LOG.debug("解析成功，已存储信息至session，账号="+serverid+",房间号="+result.getF_cc_obligate1()+",组织id="+pmsOrgVoList.get(0).getF_po_id());
			}
		}else{
			LOG.debug("跳过解析，未携带业务账号");
		}
		return resultMap;
	}

    public Map<String,Object> selectUserBillList(UserBillVo userBillVo, int curPage,
                                                 int pageSize) {
        if(!"null".equals(userBillVo.getBeginDate()) &&!"".equals(userBillVo.getBeginDate()) ) {
            userBillVo.setBeginDate(userBillVo.getBeginDate()+" 00:00:00");
        }
        if(!"null".equals(userBillVo.getEndDate()) &&!"".equals(userBillVo.getEndDate()) ) {
            userBillVo.setEndDate(userBillVo.getEndDate()+" 23:59:59");
        }
        Map<String,Object> map = new HashMap<String, Object>();
        Integer rowCount = wchatHotelDao.selectUserBillCount(userBillVo);
        map.put("total", rowCount);
        if(rowCount > 0 && curPage > 0 && pageSize > 0) {
            map.put("firstResult", (curPage-1)*pageSize);
            map.put("maxResults", pageSize);
            map.put("f_ub_serverid", userBillVo.getF_ub_serverid());
            map.put("f_ub_obligate1", userBillVo.getF_ub_obligate1());
            map.put("f_ub_productid", userBillVo.getF_ub_productid());
            map.put("f_ub_parentid", userBillVo.getF_ub_parentid());
            map.put("f_ub_productCode",userBillVo.getF_ub_productCode());
            map.put("f_ub_type", userBillVo.getF_ub_type());
            map.put("types", userBillVo.getTypes());
            map.put("f_ui_resolution", userBillVo.getF_ui_resolution());
            map.put("beginDate", userBillVo.getBeginDate());
            map.put("endDate", userBillVo.getEndDate());
            map.put("f_ub_source", userBillVo.getF_ub_source());
            map.put("f_ub_orgId", userBillVo.getF_ub_orgId());
            List<UserBillVo> results = wchatHotelDao.selectUserBillList(map);
            map.put("rows", results);
        } else {
            map.put("rows", new ArrayList<UserBillVo>());
        }
        return map;
    }
    public Map<String,Object> selectExcelList(UserBillVo userBillVo) {
        if(!"null".equals(userBillVo.getBeginDate()) &&!"".equals(userBillVo.getBeginDate()) ) {
            userBillVo.setBeginDate(userBillVo.getBeginDate()+" 00:00:00");
        }
        if(!"null".equals(userBillVo.getEndDate()) &&!"".equals(userBillVo.getEndDate())) {
            userBillVo.setEndDate(userBillVo.getEndDate()+" 23:59:59");
        }
        Map<String,Object> map = new HashMap<String, Object>();
        Integer rowCount = wchatHotelDao.selectExcelCount(userBillVo);
        map.put("total", rowCount);
        if(rowCount > 0) {
            map.put("f_ub_serverid", userBillVo.getF_ub_serverid());
            map.put("f_ub_obligate1", userBillVo.getF_ub_obligate1());
            map.put("f_ub_productid", userBillVo.getF_ub_productid());
            map.put("f_ub_parentid", userBillVo.getF_ub_parentid());
            map.put("f_ub_productCode",userBillVo.getF_ub_productCode());
            map.put("f_ub_type", userBillVo.getF_ub_type());
            map.put("types", userBillVo.getTypes());
            map.put("f_ui_resolution", userBillVo.getF_ui_resolution());
            map.put("beginDate", userBillVo.getBeginDate());
            map.put("endDate", userBillVo.getEndDate());
            map.put("f_ub_source", userBillVo.getF_ub_source());
            map.put("f_ub_orgId", userBillVo.getF_ub_orgId());
            List<UserBillVo> results = wchatHotelDao.selectExcelList(map);
            map.put("rows", results);
        } else {
            map.put("rows", new ArrayList<UserBillVo>());
        }
        return map;
    }
    public List<UserBillVo> parseExcelListUserBillVo(List<UserBillVo> list) {
        if(list!=null&&list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                try {
                    int f_ub_type=list.get(i).getF_ub_type();
                    String f_ub_type_text="";
                    switch(f_ub_type){
                        case 10 :
                            f_ub_type_text="分成-订购";
                            break;
                        case 11 :
                            f_ub_type_text="分成-退订";
                            break;
                        default :
                            f_ub_type_text="未知";
                    }
                    list.get(i).setF_ub_type_text(f_ub_type_text);
                    double f_ub_fee=Double.parseDouble(list.get(i).getF_ub_fee());
                    DecimalFormat df = new DecimalFormat("0.00");//格式化
                    list.get(i).setF_ub_fee(""+df.format((double)((int)f_ub_fee)/100));//截取2位小数
                    list.get(i).setF_ub_divide_fee(""+df.format((double)((int)(f_ub_fee/4))/100));//截取2位小数
                }catch (Exception e){
                    LOG.error("计算分成收益失败",e);
                }
            }
        }
        return list;
    }
    public List<StatDataOndemandVo> parseExcelListStatDataOndemandVo(List<StatDataOndemandVo> list) {
        if(list!=null&&list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                try {
                    BigDecimal o = new BigDecimal("400");

                    BigDecimal f_sdo_order_amount=list.get(i).getF_sdo_order_amount();
                    list.get(i).setF_sdo_order_amount(f_sdo_order_amount.divide(o,2));//截取2位小数

                    BigDecimal f_sdo_unsubscribe_amount=list.get(i).getF_sdo_unsubscribe_amount();
                    list.get(i).setF_sdo_unsubscribe_amount(f_sdo_unsubscribe_amount.divide(o,2));//截取2位小数

                    BigDecimal f_sdo_final_settlement_amount=list.get(i).getF_sdo_final_settlement_amount();
                    list.get(i).setF_sdo_final_settlement_amount(f_sdo_final_settlement_amount.divide(o,2));//截取2位小数
                }catch (Exception e){
                    LOG.error("计算分成收益失败",e);
                }
            }
        }
        return list;
    }

	public static void main(String[] args){
		PmsOrgVo poVo = new PmsOrgVo();
		poVo.setF_po_id(59);
		poVo.setF_po_name("测试酒店");

		HotelUserNeedsVo hotelUserNeedsVo = new HotelUserNeedsVo();
		hotelUserNeedsVo.setF_hsn_id("123");
		hotelUserNeedsVo.setF_hsn_type(3);
		hotelUserNeedsVo.setF_hsn_detail("加裤子一件");
		hotelUserNeedsVo.setF_hsn_zxtype("2");
		hotelUserNeedsVo.setF_hsn_roonNum("1001");
		hotelUserNeedsVo.setF_hsn_zxdate("2020-02-02 00:02:02");
		hotelUserNeedsVo.setF_hsn_price(new BigDecimal(120));
		new WechatHotelService().insertHotelUserNeedsVo(poVo, hotelUserNeedsVo);
	}

	public String getPrintSN(HotelUserNeedsVo hotelUserNeedsVo){
		String sn = "";
		int f_ccc_catalogid = 0;
		switch (hotelUserNeedsVo.getF_hsn_type()){
			case 1:
				f_ccc_catalogid = CATALOG_KFFW;
				break;
			case 2:
				f_ccc_catalogid = CATALOG_KFFW;
				break;
			case 3:
				f_ccc_catalogid = CATALOG_KFFW;
				break;
			case 4:
				f_ccc_catalogid = CATALOG_TSJY;
				break;
			case 5:
				f_ccc_catalogid = CATALOG_KFFW;
				break;
			case 6:
				f_ccc_catalogid = CATALOG_KFFW;
				break;
			case 7:
				f_ccc_catalogid = CATALOG_KFFW;
				break;
			case 8:
				f_ccc_catalogid = CATALOG_KFFW;
				break;
			case 11:
				f_ccc_catalogid = CATALOG_DCFW;
				break;
			case 12:
				f_ccc_catalogid = CATALOG_KFFW;
				break;
			case 13:
				f_ccc_catalogid = CATALOG_KFFW;
				break;
			case 14:
				f_ccc_catalogid = CATALOG_GWFW;
				break;
			default:
				f_ccc_catalogid = CATALOG_KFFW;
				break;
		}
		//获取sn
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_ccc_catalogid", f_ccc_catalogid);
		map.put("f_cc_status",1);
		map.put("f_cc_companyid", hotelUserNeedsVo.getF_hsn_orgId()+"");
		map.put("firstResult", 0);
		map.put("maxResults", 1);
		List<CmContentVo> results = contentDao.selectBindContent(map);
		if(!results.isEmpty()){
			sn = results.get(0).getF_cc_obligate1();
		}
		return sn;
	}

	public Map<String,Object> addMerchantsMessage(JSONObject obj) {
		Map<String,Object> map=new HashMap<String, Object>();
		String name = obj.getString("name");
		String phone = obj.getString("phone");
		String address = obj.getString("address");
		PmsOrgVo pmsOrgVo = new PmsOrgVo();
		pmsOrgVo.setF_po_name(name);
		pmsOrgVo.setF_po_desc(phone);
		pmsOrgVo.setF_po_address_detail(address);

		pmsOrgVo.setF_po_code("");
		pmsOrgVo.setF_po_parentId(4743);
		pmsOrgVo.setF_po_status(1);

		map = pmsServiceBean.addOrg(pmsOrgVo,"","","");
		if(!Boolean.parseBoolean(map.get("success").toString())){
			return map;
		}
		int f_po_id = pmsOrgVo.getF_po_id();
		LOG.debug("组织id："+f_po_id);
		String intro = obj.getString("intro");
		if(StringUtils.isNotEmpty(intro)){
			CmContentVo cmContentVo = new CmContentVo();
			cmContentVo.setF_cc_status(2);
			cmContentVo.setF_cc_name("商户介绍");
			cmContentVo.setF_cc_type(4);
			cmContentVo.setF_cc_desc(intro);
			cmContentVo.setF_cc_onlinedate("2020-11-23 00:00:00");
			cmContentVo.setF_cc_offlinedate("2099-11-23 00:00:00");
			cmContentVo.setF_cc_programatype("1");
			cmContentVo.setF_cc_interfaceType(1);
			cmContentVo.setF_cc_end(1);
			cmContentVo.setF_cc_seriescount(1);

			cmContentVo.setF_cc_companyid(f_po_id+"");
			map = insertContent(null,cmContentVo,390,"");
			LOG.debug("商户介绍-内容id："+cmContentVo.getF_cc_id());
			if(!Boolean.parseBoolean(map.get("success").toString())){
				return map;
			}
		}

		String pic = obj.getString("pic");
		LOG.debug("picJson:"+pic);
		JSONArray jsonArray = JSONArray.parseArray(pic);
		JSONObject mtObj = jsonArray.getJSONObject(0);
		JSONObject kjObj = jsonArray.getJSONObject(1);
		JSONObject ggObj = jsonArray.getJSONObject(2);
		JSONObject yltObj = jsonArray.getJSONObject(3);
		JSONObject ewmObj = jsonArray.getJSONObject(4);

		JSONArray mtPicArr = mtObj.getJSONArray("serverId");
		if(!mtPicArr.isEmpty()&&mtPicArr.size()>0){
			CmContentVo cmContentVo = new CmContentVo();
			cmContentVo.setF_cc_status(2);
			cmContentVo.setF_cc_name("商户美图");
			cmContentVo.setF_cc_type(4);
			cmContentVo.setF_cc_onlinedate("2020-11-23 00:00:00");
			cmContentVo.setF_cc_offlinedate("2099-11-23 00:00:00");
			cmContentVo.setF_cc_programatype("1");
			cmContentVo.setF_cc_interfaceType(1);
			cmContentVo.setF_cc_end(1);
			cmContentVo.setF_cc_seriescount(1);

			cmContentVo.setF_cc_companyid(f_po_id+"");
			String picJson = "";
			for(int i=0;i<mtPicArr.size();i++){
				picJson = "[{\"id\":\"2\",\"picId\":\"\",\"picUrl\":\"PIC/imgs/upload/wx/"+mtPicArr.get(i)+".jpg\"}]";
				map = insertContent(null,cmContentVo,391,picJson);
				LOG.debug("商户美图-内容id："+cmContentVo.getF_cc_id());
				if(!Boolean.parseBoolean(map.get("success").toString())){
					return map;
				}
			}
		}

		JSONArray kjPicArr = kjObj.getJSONArray("serverId");
		if(!kjPicArr.isEmpty()&&kjPicArr.size()>0){
			CmContentVo cmContentVo = new CmContentVo();
			cmContentVo.setF_cc_status(2);
			cmContentVo.setF_cc_name("商户开机图");
			cmContentVo.setF_cc_type(4);
			cmContentVo.setF_cc_onlinedate("2020-11-23 00:00:00");
			cmContentVo.setF_cc_offlinedate("2099-11-23 00:00:00");
			cmContentVo.setF_cc_programatype("1");
			cmContentVo.setF_cc_interfaceType(1);
			cmContentVo.setF_cc_end(1);
			cmContentVo.setF_cc_seriescount(1);

			cmContentVo.setF_cc_companyid(f_po_id+"");
			String picJson = "";
			for(int i=0;i<kjPicArr.size();i++){
				picJson = "[{\"id\":\"2\",\"picId\":\"\",\"picUrl\":\"PIC/imgs/upload/wx/"+kjPicArr.get(i)+".jpg\"}]";
				map = insertContent(null,cmContentVo,399,picJson);
				LOG.debug("商户开机图-内容id："+cmContentVo.getF_cc_id());
				if(!Boolean.parseBoolean(map.get("success").toString())){
					return map;
				}
			}
		}

		JSONArray ggPicArr = ggObj.getJSONArray("serverId");
		if(!ggPicArr.isEmpty()&&ggPicArr.size()>0){
			CmContentVo cmContentVo = new CmContentVo();
			cmContentVo.setF_cc_status(2);
			cmContentVo.setF_cc_name("商户广告图");
			cmContentVo.setF_cc_type(4);
			cmContentVo.setF_cc_onlinedate("2020-11-23 00:00:00");
			cmContentVo.setF_cc_offlinedate("2099-11-23 00:00:00");
			cmContentVo.setF_cc_programatype("1");
			cmContentVo.setF_cc_interfaceType(1);
			cmContentVo.setF_cc_end(1);
			cmContentVo.setF_cc_seriescount(1);
			cmContentVo.setF_cc_desc("#");

			cmContentVo.setF_cc_companyid(f_po_id+"");
			String picJson = "";
			for(int i=0;i<ggPicArr.size();i++){
				picJson = "[{\"id\":\"2\",\"picId\":\"\",\"picUrl\":\"PIC/imgs/upload/wx/"+ggPicArr.get(i)+".jpg\"}]";
				map = insertContent(null,cmContentVo,400,picJson);
				LOG.debug("商户广告图-内容id："+cmContentVo.getF_cc_id());
				if(!Boolean.parseBoolean(map.get("success").toString())){
					return map;
				}
			}
		}

		JSONArray yltPicArr = yltObj.getJSONArray("serverId");
		if(!yltPicArr.isEmpty()&&yltPicArr.size()>0){
			CmContentVo cmContentVo = new CmContentVo();
			cmContentVo.setF_cc_status(2);
			cmContentVo.setF_cc_name(name);
			cmContentVo.setF_cc_type(4);
			cmContentVo.setF_cc_onlinedate("2020-11-23 00:00:00");
			cmContentVo.setF_cc_offlinedate("2099-11-23 00:00:00");
			cmContentVo.setF_cc_programatype("1");
			cmContentVo.setF_cc_interfaceType(1);
			cmContentVo.setF_cc_end(1);
			cmContentVo.setF_cc_seriescount(1);

			cmContentVo.setF_cc_companyid(f_po_id+"");
			String picJson = "";
			String type = obj.getString("type");
			int catalogid = 0;
			if("火锅".equals(type)){
				catalogid=394;
			}else if("私房菜".equals(type)){
				catalogid=395;
			}else if("烧烤".equals(type)){
				catalogid=396;
			}else if("酒吧".equals(type)){
				catalogid=397;
			}else if("休闲".equals(type)){
				catalogid=398;
			}
			for(int i=0;i<yltPicArr.size();i++){
				picJson = "[{\"id\":\"2\",\"picId\":\"\",\"picUrl\":\"PIC/imgs/upload/wx/"+yltPicArr.get(i)+".jpg\"}]";
				map = insertContent(null,cmContentVo,catalogid,picJson);
				LOG.debug("商户预览图-内容id："+cmContentVo.getF_cc_id());
				if(!Boolean.parseBoolean(map.get("success").toString())){
					return map;
				}
			}
		}

		JSONArray ewmPicArr = ewmObj.getJSONArray("serverId");
		if(!ewmPicArr.isEmpty()&&ewmPicArr.size()>0){
			CmContentVo cmContentVo = new CmContentVo();
			cmContentVo.setF_cc_status(2);
			cmContentVo.setF_cc_name("商户二维码图");
			cmContentVo.setF_cc_type(4);
			cmContentVo.setF_cc_onlinedate("2020-11-23 00:00:00");
			cmContentVo.setF_cc_offlinedate("2099-11-23 00:00:00");
			cmContentVo.setF_cc_programatype("1");
			cmContentVo.setF_cc_interfaceType(1);
			cmContentVo.setF_cc_end(1);
			cmContentVo.setF_cc_seriescount(1);

			cmContentVo.setF_cc_companyid(f_po_id+"");
			String picJson = "";
			for(int i=0;i<ewmPicArr.size();i++){
				picJson = "[{\"id\":\"2\",\"picId\":\"\",\"picUrl\":\"PIC/imgs/upload/wx/"+ewmPicArr.get(i)+".jpg\"}]";
				map = insertContent(null,cmContentVo,392,picJson);
				LOG.debug("商户二维码图-内容id："+cmContentVo.getF_cc_id());
				if(!Boolean.parseBoolean(map.get("success").toString())){
					return map;
				}
			}
		}

		return map;
	}

	public PmsUserVo findUserOrg(PmsUserVo puv){
		if(puv==null||puv.getF_pu_name()==null||puv.getF_pu_pwd()==null)
			return null;
		else
			return wchatHotelDao.findUserOrg(puv);
	}

	public PmsUserVo findUserOrgByPhone(PmsUserVo puv){
		if(puv==null||puv.getF_pu_phone()==null)
			return null;
		else
			return wchatHotelDao.findUserOrg(puv);
	}

	public PmsUserVo findUserOrgByName(PmsUserVo puv){
		if(puv==null||puv.getF_pu_name()==null)
			return null;
		else
			return wchatHotelDao.findUserOrgByName(puv);
	}

	public Map<String,Object> updateHotelIntroduceMessage(JSONObject obj) {
		Map<String,Object> map=new HashMap<String, Object>();
		String f_cc_name = obj.getString("f_cc_name");
		String f_cc_desc = obj.getString("f_cc_desc");
		String f_cc_id = obj.getString("f_cc_id");

		CmContentVo cmContentVo = new CmContentVo();
		cmContentVo.setF_cc_status(2);
		cmContentVo.setF_cc_name(f_cc_name);
		cmContentVo.setF_cc_desc(f_cc_desc);
		cmContentVo.setF_cc_id(Integer.parseInt(f_cc_id));
		int flag = wchatHotelDao.updateContent(cmContentVo);
		if(flag<1){
			map.put("success",false);
		}else{
			map.put("success",true);
		}
		return map;
	}

	public Map<String,Object> loginOut(PmsUserVo userVo) {
		Map<String,Object> map=new HashMap<String, Object>();
		int flag = wchatHotelDao.loginOut(userVo);
		if(flag<1){
			map.put("success",false);
		}else{
			map.put("success",true);
		}
		return map;
	}
}